package com.hd123.rumba.gwt.widget.client;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class FilterDialog extends DialogBox implements ClickListener {
  private final Button okButton, cancelButton, clearButton;
  private boolean ok = false;

  /**
   * 构建过滤条件的界面元素
   * @param workPanel
   */
  protected abstract void initContent(VerticalPanel workPanel);
  /**
   * 根据界面内容构建过滤条件
   */
  protected abstract void constructFilter();
  /**
   * 清除过滤条件的值和界面显示
   */
  protected abstract void clearFilter();
  
  public FilterDialog() {
    super(false, true);
    setText("请输入筛选条件:");

    VerticalPanel workPanel = new VerticalPanel();
    workPanel.setSpacing(4);
    initContent(workPanel);

    okButton = new Button(" 确定 ", this);
    cancelButton = new Button(" 取消 ", this);
    clearButton = new Button(" 清除 ", this);
    HorizontalPanel actionPanel = new HorizontalPanel();
    actionPanel.setSpacing(2);
    actionPanel.setStyleName("jdgwt-panel");
    actionPanel.add(okButton);
    actionPanel.add(cancelButton);

    HorizontalPanel actionOuter = new HorizontalPanel();
    actionOuter.setWidth("100%");
    actionOuter.add(clearButton);
    actionOuter.setCellHorizontalAlignment(clearButton, HorizontalPanel.ALIGN_LEFT);
    actionOuter.add(actionPanel);
    actionOuter.setCellHorizontalAlignment(actionPanel, HorizontalPanel.ALIGN_RIGHT);

    DockPanel dock = new DockPanel();
    dock.setSpacing(4);
    dock.setWidth("100%");
    dock.add(workPanel, DockPanel.CENTER);
    dock.add(actionOuter, DockPanel.SOUTH);
    setWidget(dock);
  }

  public void onClick(Widget sender) {
    if (sender == clearButton) {
      clearFilter();
    } else {
      ok = sender == okButton;
      if (ok) {
        constructFilter();
      }
      hide();
    }
  }

  public boolean isOk() {
    return ok;
  }
}
